import app, { xlsxCfgNames } from "../../../../app";
import { att_preference, face_attributes } from "../../../../shared/interface";

export class _Cls_Att implements face_attributes {
    /**
         * 生命值
         */
    Hp_Max: number = 0;
    /**
     * 生命恢复速度
     */
    Hp_res: number = 0;
    /**
     * 魔法值
     */
    Mp_Max: number = 0;
    /**
     * 蓝量恢复速度
     */
    Mp_res: number = 0;
    /**
     * 最大物攻
     */
    Physical_Attack_max: number = 0;
    /**
     * 最小物攻
     */
    Physical_Attack_min: number = 0;
    /**
     * 物理吸血
     */
    Physical_Ls: number = 0;
    /**
     * 暴击率
     */
    Physical_crit: number = 0;
    /**
     * 物理防御
     */
    Physical_defense: number = 0;
    /**
     * 最大魔攻
     */
    Magic_Attack_max: number = 0;
    /**
    * 魔法吸血
    */
    Magic_Ls: number = 0;
    /**
     * 魔法防御
     */
    Magic_defense: number = 0;
    /**
     * 魔法暴击率
     */
    Magic_crit: number = 0;
    /**
    * 最小魔攻
    */
    Magic_Attack_min: number = 0;
    /**
     * 攻击速度
     */
    Attack_Speed: number = 0;
    // /**
    //  * 宠物数量
    //  */
    // Pet_cont: number = 0;
    /**
     * 召唤数量
     */
    Calls_cont: number = 0;
    /**
     * 召唤等级最高
     */
    Calls_leveMax: number = 0;
    /**
    * 召唤等级最低
    */
    Calls_leveMin: number = 0;
    /**
     * 召唤评分
     */
    Calls_sore: number = 0;
    // 白盾
    _wall_hp: number = 0;

    constructor(data?: any) {
        if (data) {
            this.reset(data)
        }
    }
    getScore() {
        let score = 0;
        const attCfg = app.xlsxCfgMap.get(xlsxCfgNames.属性配置) as Map<string, any>;
        for (const key in this) {
            if (key.startsWith('_')) {
                continue;
            }
            if(!attCfg.get(key)){
                continue;
            }
            score += attCfg.get(key).value * (this[key] as number)
        }
        return score;
    }
    random(score: number, type: att_preference) {
        // let total = Math.ceil(score);
        // const arry = [...(app.xlsxCfgMap.get(xlsxCfgNames.属性配置) as Map<string, any>).values()]
        app.xlsxCfgMap.get(xlsxCfgNames.属性偏好配置)
        let attInfo = app.getCfg(xlsxCfgNames.属性偏好配置).get(type.toString())
        let points = app.randomGroupPoint(score,attInfo.com.length)
        for (let i = 0; i < attInfo.com.length; i++) {
            const element = attInfo.com[i];
            const value = app.getAttVaule(element)
            // total = this.random_key(element, 1 / value, total);
            this.setKey(element,points[i] * 1 / value)
        }

        // for (let i = 0; i < arry.length; i++) {
        //     const element = arry[i];
        //     if (typeof (this[element.id]) == 'undefined') {
        //         continue;
        //     }
        //     total = this.random_key(element.id, 1 / element.value, total);
        //     if (total <= 0) {
        //         break;
        //     }
        // }
    }
    dbsave() {
        let obj: any = {}
        for (const key in this) {
            if (key.startsWith('_')) {
                continue;
            }
            if (typeof (this[key]) == 'number' && !this[key]) {
                continue;
            }
            obj[key] = this[key];
        }
        return obj
    }
    private random_key(key: string, power: number, score: number) {
        if (score < 1) {
            this[key] = 0;
            return 0
        }
        let randomVal = app.generateRandom(1, score, score)
        let supVal = score - randomVal;
        this[key] = randomVal * power;
        return supVal;
    }
    private setKey(key: string, value: number){
        this[key] = value;
    }
    clearAll() {
        let z: any = 0;
        for (const key in this) {
            if (typeof (this[key]) == 'number') {
                this[key] = z;
            }
        }
    }
    setMultiple_data(data: any, rang: number = 1) {
        for (const key in data) {
            if (typeof (this[key]) != 'number') {
                continue;
            }
            (this[key] as any) += (data[key] / rang as number) * (this[key] as number);
        }
        this.fix_maxNum();
        return this;
    }
    getMultiple_data(data: any) {
        let att = new _Cls_Att();
        att.reset(this);
        for (const key in data) {
            if (typeof (att[key]) != 'number') {
                continue;
            }
            (att[key] as any) = (data[key] as number) * (this[key] as number);
        }
        return att;
    }
    getMultiple_all(num: number) {
        let att = new _Cls_Att();
        for (const key in this) {
            if (typeof (this[key]) != 'number') {
                continue;
            }
            // (this[key] as any) = (this[key] as number) * num;
            att.add({ [key]: (this[key] as number) * num })

        }
        return att;
    }
    private fix_maxNum() {
        if (this.Attack_Speed > 15) {
            this.Attack_Speed = 15;
        }
    }
    reset(data: any) {
        for (const key in data) {
            if (typeof (this[key]) != 'number') {
                continue;
            }
            this[key] = data[key]
        }
    }
    add(data: any) {
        for (const key in data) {
            if (typeof (data[key]) == 'number') {
                this[key] += data[key]
                if (isNaN(this[key])) {
                    debugger;
                }
            }
        }
        this.fix_maxNum();
    }
}